import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/afterSales/api/v1';

const getInitdesc = formatMessage({
    id: 'api.message.getInitdesc',
    defaultMessage: '初始化'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询详情'
});
const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增'
});
const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '修改'
});
const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废'
});
const getPartListDesc = formatMessage({
    id: 'api.message.getPartListDesc',
    defaultMessage: '查询备件'
});
const getDutyUnitListDesc = formatMessage({
    id: 'api.message.getDutyUnitListDesc',
    defaultMessage: '查询供应商'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/qualityArbitration/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitdesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/qualityArbitrations?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/qualityArbitrations/${id}`, null, dto.Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 新增
export const addApplyReport = data => request(`${baseUrl}/qualityArbitrations`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));
//修改
export const updateApplyReport = (id, data) => request(`${baseUrl}/qualityArbitrations/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));
// 作废
export const abandonData = (id, rowVersion) => request(`${baseUrl}/qualityArbitrations/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
// 查询供应商
export const getDutyUnit = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/dutyUnits/base?${queryString}`, null, dto.DutyUnitList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDutyUnitListDesc));
};
// 查询备件
export const getPart = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forDutyUnitReplace?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartListDesc));
};
//导入模板
export const importTemp = () => request(`${baseUrl}/qualityArbitrations/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
